using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Mapping;
using SolanumSolstice.Buildings;

namespace SolanumSolstice.Units
{
    public class Civilian : Human
    {
        protected bool isGathering;
        protected bool isDepositing;
        protected bool isRepairing;
        protected int woodHolding;
        protected int foodHolding;
        protected int maxCarry;
        protected float gatherTime;
        protected int repairPower;
        protected float repairTime;
        protected Building targetGatherBuilding;
        protected Building targetDepositBuilding;
        protected RoadBlock targetRoadBlock;

        #region Accessors
        public bool IsGathering
        {
            get
            {
                return isGathering;
            }
            set
            {
                isGathering = value;
            }
        }

        public bool IsDepositing
        {
            get
            {
                return isDepositing;
            }
            set
            {
                isDepositing = value;
            }
        }

        public bool IsRepairing
        {
            get
            {
                return isRepairing;
            }
            set
            {
                isRepairing = value;
            }
        }

        public float GatherTime
        {
            get
            {
                return gatherTime;
            }
            set
            {
                gatherTime = value;
            }
        }

        public int WoodHolding
        {
            get
            {
                return woodHolding;
            }
            set
            {
                woodHolding = value;
            }
        }

        public int FoodHolding
        {
            get
            {
                return foodHolding;
            }
            set
            {
                foodHolding = value;
            }
        }

        public int MaxCarry
        {
            get
            {
                return maxCarry;
            }
            set
            {
                maxCarry = value;
            }
        }

        public Building TargetGatherBuilding
        {
            get
            {
                return targetGatherBuilding;
            }
            set
            {
                targetGatherBuilding = value;
            }
        }

        public Building TargetDepositBuilding
        {
            get
            {
                return targetDepositBuilding;
            }
            set
            {
                targetDepositBuilding = value;
            }
        }

        public int RepairPower { get {return repairPower; } set {repairPower=value;}}
        #endregion


        public override Object Clone()
        {
            Civilian u = new Civilian(this.content, this.position);

            u.weapon = this.weapon;
            u.hitPoints = this.hitPoints;
            u.maxHitPoints = this.maxHitPoints;
            u.recoveryTime = this.recoveryTime;
            u.initialRecoveryTime = this.initialRecoveryTime;
            u.position = this.position;
            u.velocity = this.velocity;
            u.sightRadius = this.sightRadius;
            u.attackRadius = this.attackRadius;
            u.attackPower = this.attackPower;
            u.isSelected = this.isSelected;
            u.isDead = this.isDead;
            u.isDying = this.isDying;
            u.isZombie = this.isZombie;
            u.inBuilding = this.inBuilding;
            u.isAttacking = this.isAttacking;
            u.actionSelected = this.actionSelected;
            u.actionReady = this.actionReady;
            u.rand = this.rand;
            u.actions = this.actions;
            u.currentActionIndex = this.currentActionIndex;
            u.targetReached = this.targetReached;
            u.direction = new Vector2(this.direction.X, this.direction.Y);
            u.unitClass = this.unitClass;
            u.timeCost = this.timeCost;
            u.spawnCost = this.spawnCost;
            
            u.woodHolding = this.woodHolding;
            u.foodHolding = this.foodHolding;
            u.targetGatherBuilding=this.targetGatherBuilding;
            u.targetDepositBuilding=this.targetDepositBuilding;
            u.targetRoadBlock=this.targetRoadBlock;
            u.isGathering = this.isGathering;
            u.isRepairing = this.isRepairing;
            u.isDepositing = this.isDepositing;
            u.gatherTime = this.gatherTime;
            u.maxCarry = this.maxCarry;
            u.repairPower = this.repairPower;
            u.repairTime = this.repairTime;
            /*u.texture = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\civ_civilian_sheet");
            u.hudTexture = content.Load<Texture2D>("Content\\Txtr\\Units\\HUD\\copHUD");
            u.selectedCircle = content.Load<Texture2D>("Content\\Txtr\\Units\\selectedUnitCircle");
            u.emptyBar = content.Load<Texture2D>("Content\\Txtr\\Units\\EmptyBar");
            u.fullBar = content.Load<Texture2D>("Content\\Txtr\\Units\\FullBarWhite");*/
            u.frameCount = this.frameCount;
            u.frameIncrementer = this.frameIncrementer;
            u.weaponFrameCount = this.weaponFrameCount;
            u.dyingFrameCount = this.dyingFrameCount;
            u.groupOffset = new Vector2(this.groupOffset.X, this.groupOffset.Y);

            return (Object)u;
         }

        public Civilian(ContentManager content, Vector2 position)
            : base(content, position)
        {

            texture = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\civ_civilian_sheet");
            hudTexture = content.Load<Texture2D>("Content\\Txtr\\Units\\HUD\\civHUD");
            this.hitPoints = 10;
            maxHitPoints = hitPoints;
            velocity = 1.5f;
            sightRadius = 3;
            attackPower = 1;
            unitClass = UnitClass.Civilian;
            isGathering = false;
            isRepairing = false;
            isDepositing = false;
            GatherTime = 10;
            maxCarry = 4;

            repairPower = 9;
            repairTime = 2;

            spawnCost = 8;
            timeCost = 8;

        }


        public override void Update(GameTime gameTime, List<Unit> enemiesInRange, List<Unit> friendsInRange)
        {
            base.Update(gameTime, enemiesInRange, friendsInRange);

            
            if (ActionReady == true)
            {

                if (isGathering == true && targetReached == true && inBuilding == false)
                {
                    targetGatherBuilding.EnterBuilding(this);
                }

                if (isDepositing == true && targetReached == true && inBuilding == false)
                {
                    Base baseDepot = (Base)targetDepositBuilding;
                    baseDepot.DepositFood(foodHolding, this);
                    baseDepot.DepositWood(woodHolding, this);

                }

                if (isRepairing == true && targetReached == true)
                {
                    targetRoadBlock.healDamage(rand.Next(repairPower));

                    if (targetRoadBlock.HitPoints == targetRoadBlock.MaxHitPoints)
                    {
                        isRepairing = false;
                    }

                    actionReady = false;
                    recoveryTime = repairTime;
                    InitialRecoveryTime = repairTime;
                }
            }
        }

        public override void SetTarget(Vector2 target)
        {
            base.SetTarget(target);
            isDepositing = false;
            isGathering = false;
            isRepairing = false;
            //targetGatherBuilding = null;
            //targetDepositBuilding = null;
        }

        public void SetGatherTarget(Vector2 target, Building targetBuild)
        {
            SetTarget(targetBuild.Entrance);
            isGathering = true;
            isDepositing = false;
            targetGatherBuilding = targetBuild;
            
        }

        public void SetDepositTarget(Vector2 target, Building targetBuild)
        {
            SetTarget(targetBuild.Entrance);
            isGathering = false;
            isDepositing = true;
            targetDepositBuilding = targetBuild;
        }

        public void SetRepairTarget(RoadBlock roadBlock)
        {

            SetTarget(roadBlock.Position);
            isGathering = false;
            isDepositing = false;
            isRepairing = true;

            targetRoadBlock = roadBlock;

            
        }

        public void GatherFood()
        {
            foodHolding = maxCarry;
        }

        public void GatherWood()
        {
            woodHolding = maxCarry;
        }


    }
}


